package com.laicunba.ext.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import com.laicunba.constant.AttributeConstant;
import com.laicunba.ext.web.init.CookieUtil;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.UserService;
import com.laicunba.service.gaoda.GaodaService;
import com.laicunba.util.CacheUtil;
import com.laicunba.util.StringUtil;

public class SuiCunBaoBaseController extends BaseController {
	@Autowired
	private GaodaService gaodaService;
	@Autowired
	private UserService userService;

	protected UserDO getCurrentUser(HttpServletRequest request) {
		// app用户
		if (StringUtil.isNotEmpty(request.getParameter("app_token"))) {
			UserDO userDO = getUserByAppToken(request.getParameter("app_token"));
			if (userDO != null) {
				return userDO;
			}
		}

		// h5登录
		String token = CookieUtil.get(request, "token");
		if (StringUtil.isNotEmpty(token)) {
			String cachedUserId = CacheUtil.getAttribute(AttributeConstant.USER_LOGIN_TOKEN + token);
			if (StringUtil.isNotEmpty(cachedUserId)) {
				UserDO userDO = userService.get(UserDO.class, cachedUserId);
				if (userDO != null) {
					return userDO;
				}
			}
		}

		return null;
	}
}
